import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from "typeorm";

/**
 * 菜单结构关系表
 */
@Entity('test_menu_closure')
export class MenuClosure {

  @PrimaryGeneratedColumn()
  id: number;

  // 菜单id
  @Column({ name: 'descendant_id' })
  descendantId: number;

  // 上级目录id
  @Column({ name: 'ancestor_id' })
  ancestorId: number;

  @OneToOne(() => MenuEntity, menu => menu.closure)
  @JoinColumn({ name: 'test_menu_closure_descendant_id' })
  menuId: any;
}

/**
 * 菜单表
 */
@Entity('test_menu')
export class MenuEntity {
  // 菜单ID
  @PrimaryGeneratedColumn()
  id: number;

  // 菜单名称
  @Column({ name: 'menu_name' })
  menuName: string;

  // 节点类型
  @Column()
  type: string;

  // 路由地址
  @Column({ name: 'router_path' })
  path: string;

  // 路由名称
  @Column({ name: 'router_name' })
  routerName: string;

  // 入口文件地址
  @Column({ name: 'component' })
  component: string;

  // icon
  @Column()
  icon: string;

  // 排序
  @Column({ name: 'serial_no' })
  serialNo: number;

  // 是否启用
  @Column({ name: 'enabled' })
  enabled: boolean;

  // 创建时间
  @Column({ name: 'create_time' })
  createTime: string;

  // 是否缓存
  @Column({ name: 'keep_alive' })
  keepAlive: boolean;

  // 虚拟字段用于映射父节点id
  @Column({ select: false })
  parentId: number;

  // 定义关联关系
  @OneToOne(() => MenuClosure, closure => closure.menuId)
  closure: MenuClosure;
}